/*
 * Created on 2005-1-7
 *
 */
package com.taobao.common.dao.persistence;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.taobao.common.dao.persistence.exception.DBRouterException;

/**
 * @author xingdian
 * @version $Id: SqlMapBaseDAOSupport.java,v 1.2 2005/07/29 08:02:36 sa Exp $
 */
public class SqlMapBaseDAOSupport {

  // ������ݿ������ʵ��
  private Map            sqlMapList;
  private Map            sqlMapTemplateList = new HashMap();

  private DBRouteManager dbRouteManager;

  /**
   * @return Returns the dbRouteManager.
   */
  public DBRouteManager getDbRouteManager() {
    return dbRouteManager;
  }

  /**
   * @param dbRouteManager
   *          The dbRouteManager to set.
   */
  public void setDbRouteManager(DBRouteManager dbRouteManager) {
    this.dbRouteManager = dbRouteManager;
  }

  /**
   * ��ʼ��
   */
  public void init() {

    if (null == sqlMapList) {
      throw new RuntimeException("���ݿ�ӳ���ļ�����ָ����");
    }

    // ����SqlMapTemplate�б�
    // Iterator it = dbList.keySet().iterator();
    Iterator it = sqlMapList.keySet().iterator();
    String dbKey;
    while (it.hasNext()) {
      SqlMapClientTemplate sqlMT = new SqlMapClientTemplate();
      dbKey = (String) it.next();
      sqlMT.setSqlMapClient((SqlMapClient) sqlMapList.get(dbKey));
      sqlMapTemplateList.put(dbKey, sqlMT);
    }
  }

  /**
   * @return Returns the sqlMapList.
   */
  public Map getSqlMapList() {
    return sqlMapList;
  }

  /**
   * @param sqlMapList
   *          The sqlMapList to set.
   */
  public void setSqlMapList(Map sqlMapList) {
    this.sqlMapList = sqlMapList;
  }

  /**
   * @return Returns the sqlMapTemplateList.
   */
  public Map getSqlMapTemplateList() {
    return sqlMapTemplateList;
  }

  /**
   * @param sqlMapTemplateList
   *          The sqlMapTemplateList to set.
   */
  public void setSqlMapTemplateList(Map sqlMapTemplateList) {
    this.sqlMapTemplateList = sqlMapTemplateList;
  }

  /**
   * ����һ�����ݿ���б��������
   * 
   * @param dr
   * @param strId
   * @return
   * @throws DBRouterException
   */
  public Map getSqlMapTemplates(DBRoute dr, String sqlId) throws DBRouterException {
    List dbNameList = getDbRouteManager().getSqlMapTemplates(dr, sqlId);
    if (null == dbNameList) {
      return null;
    }

    Map retDbList = new HashMap();

    for (int i = 0; i < dbNameList.size(); i++) {
      String dbName = (String) dbNameList.get(i);
      Object o = sqlMapTemplateList.get(dbName);
      if (o != null) {
        retDbList.put(dbName, o);
      }
    }
    return retDbList;
  }

  /**
   * ����һ�����ݿ������ֱ�ӷ���һ��SqlMapTemplate
   * 
   * @param dr
   * @return
   * @throws DBRouterException
   */
  public SqlMapClientTemplate getSqlMapTemplate(DBRoute dr) throws DBRouterException {
    if (dr == null || dr.getXid() == null || "".equalsIgnoreCase(dr.getXid())) {
      return null;
    }

    return (SqlMapClientTemplate) sqlMapTemplateList.get(dr.getXid());
  }

  /**
   * ����·����Ϣ����ĳ����������ݿ⣬������������ݿ⣬�򷵻�Ϊ�ա�
   * 
   * @param dr
   * @param sqlId
   * @return
   * @throws DBRouterException
   */
  public String getDBName(DBRoute dr, String sqlId) throws DBRouterException {
    if (dr == null) {
      return null;
    }

    List dbNameList = getDbRouteManager().getSqlMapTemplates(dr, sqlId);

    if (null == dbNameList) {
      return null;
    }

    if (dbNameList.size() > 1) {
      return null;
    }

    return (String) dbNameList.get(0);
  }
}
